<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改密码')" />
</head>
<style>
.white-bg{justify-content: center;align-items: center;flex-direction: column;border-radius: 4px;}
.wrapper{margin: 0 auto;padding: 33px 57px 0px 58px;box-sizing:border-box;display: flex;justify-content: flex-end;align-items: center;flex-direction: column;}
.dialog-head span{padding-left:16px;lear:both;font-size: 17px}
.dialog-head i{float:right;padding-top:20px;padding-right:16px;}
#form-user-resetPwd,#form-user-resetPwd2{text-align:center;}
#form-user-resetPwd .layui-form-label {width:138px;font-size:14px;padding:4px 0 0;}
#form-user-resetPwd .layui-input-block {margin-left:138px;width:240px;text-align:right;}
#form-user-resetPwd2 .layui-form-label {width:166px;font-size:14px;padding:4px 0 0;}
#form-user-resetPwd2 .layui-input-block {margin-left:166px;width:240px;text-align:right;}
.layui-form-item{margin-bottom: 20px;margin-top:0px;float:right;}
/*.layui-form-item:nth-of-type(3){margin-bottom: 50px;}*/
.layui-form-item input{height:30px;line-height:30px;}
.require{color:#D80000;}
.layui-btn{border-radius:2px;}
</style>
<body class="white-bg">
<!--	<div class="dialog-head"><span>修改密码</span><i class="dhicon dh-lujing460"></i></div>-->
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<div class="layui-col-md12">
			<form class="layui-form" id="form-user-resetPwd" lay-filter="form-user-resetPwd">
			<input type="hidden" name="userId" th:value="${userId}">
			<input type="hidden" name="upType" th:value="${upType}">
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请输入您的旧密码：</label>
				<div class="layui-input-block">
				<input type="password" id="oldPwd" name="oldPwd" autocomplete="new-password" class="layui-input" value="">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请输入您的新密码：</label>
				<div class="layui-input-block">
				<input type="password" id="newPwd" name="newPwd" autocomplete="new-password" class="layui-input" value="">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请确认您的新密码：</label>
				<div class="layui-input-block">
				<input type="password" id="checkNewPwd" autocomplete="new-password" class="layui-input" value="">
				</div>
			</div>
			<div style="text-align: center;padding-bottom:30px;">
				<button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="btn-add">保存</button>
			</div>
			</form>
		</div>
		<div class="layui-col-md12">
			<form class="layui-form" id="form-user-resetPwd2" lay-filter="form-user-resetPwd2">
			<input type="hidden" name="userId" th:value="${userId}">
			<input type="hidden" name="upType" th:value="${upType}">
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请输入您的二级旧密码：</label>
				<div class="layui-input-block">
				<input type="password" id="oldPwd2" name="oldPwd2" class="layui-input" value="">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请输入您的二级新密码：</label>
				<div class="layui-input-block">
				<input type="password" id="newPwd2" name="newPwd2" class="layui-input" value="">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="require">*</span>请确认您的二级新密码：</label>
				<div class="layui-input-block">
				<input type="password" id="checkNewPwd2" class="layui-input" value="">
				</div>
			</div>
			<div style="text-align: center;padding-bottom:30px;">
				<button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="btn-add2">保存</button>
			</div>
			</form>
		</div>
	</div>
<th:block th:include="include :: footer" />
<script th:src="@{/js/plugins/jsencrypt/jsencrypt.min.js}"></script>
<script type="text/javascript">
	var publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKthHRNQkJ8W3E2YVQEhXrOl6GrPIhNV8VH3BTCXqwSeetVfZNLM2bpv1AcA215xDnGmKI7f8mwW5V8Z/g4sqQkCAwEAAQ==';
	//加密
	function encrypt(txt) {
		const encryptor = new JSEncrypt()
		encryptor.setPublicKey(publicKey) // 设置公钥
		return encryptor.encrypt(txt) // 对数据进行加密
	}
	layui.use(['laydate', 'form','layer','jquery'], function () {
		var $ = layui.$,layer = layui.layer,form = layui.form,postStr = [];
		form.on('submit(btn-add)', function(data){
			var check_arr = [{'kName':'oldPwd','kStr':'旧密码'},{'kName':'newPwd','kStr':'新密码'},{'kName':'checkNewPwd','kStr':'确认新密码'}],
				postUrl = "/system/user/editpwdpost";
			editpwdpost(check_arr, postUrl);
		});
		form.on('submit(btn-add2)', function(data){
			var check_arr = [{'kName':'oldPwd2','kStr':'旧二级密码'},{'kName':'newPwd2','kStr':'新二级密码'},{'kName':'checkNewPwd2','kStr':'确认新二级密码'}],
				postUrl = "/system/user/editpwd2post";
			editpwdpost(check_arr, postUrl, 2)
		});
		function editpwdpost(check_arr, url, num=''){
			var checkStatus = true;
			$.each(check_arr,function(i,v){
				if(!$("#"+v.kName).val()){
					$.modal.msgError(v.kStr+" 不能为空");
					$("#"+v.kName).css('border','1px solid red');
					$("#"+v.kName).focus()
					checkStatus = false;
					return false;
				}
			})
			if(!checkStatus)return false;
			if($("#newPwd"+num).val().length < 6){
				if(num){
					$.modal.msgError("新二级密码长度不能低于6位！");
				}else{
					$.modal.msgError("新密码长度不能低于6位！");
				}
				$("#checkNewPwd"+num).css('border','1px solid red');
				$("#checkNewPwd"+num).focus()
				checkStatus = false;
				return false;
			}
			if($("#newPwd"+num).val() != $("#checkNewPwd"+num).val()){
				if(num){
					$.modal.msgError("新二级密码和确认新二级密码输入值不相同");
				}else{
					$.modal.msgError("新密码和确认新密码输入值不相同");
				}
				$("#checkNewPwd"+num).css('border','1px solid red');
				$("#checkNewPwd"+num).focus()
				checkStatus = false;
				return false;
			}
			if($("#newPwd"+num).val() == $('[name=userId]').val()){
				$.modal.msgError("新密码不允许设置成默认密码");
				$("#checkNewPwd"+num).css('border','1px solid red');
				$("#checkNewPwd"+num).focus()
				checkStatus = false;
				return false;
			}
			if(checkStatus){
				var data = $.common.formToJSON("form-user-resetPwd"+num);
				if(num == 2){
					data.oldPwd2 = encrypt(data.oldPwd2);
					data.newPwd2 = encrypt(data.newPwd2);
					data.checkNewPwd2 = encrypt(data.checkNewPwd2);
				}else{
					data.oldPwd = encrypt(data.oldPwd);
					data.newPwd = encrypt(data.newPwd);
					data.checkNewPwd = encrypt(data.checkNewPwd);
				}
				$.operate.post(url,data,function(result){
					if(result.code == 0){
						setTimeout(function(){
							window.location.reload();
						}, 1000);
					}
				}); 
			}
			return false;
		}
	});
</script>
</body>
</html>
